<?php
/* 
	This file is Copyright Matthew Wilkinson 2009.
	
	This file is part of Oxygen.

	Oxygen is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License version 3 as published by
	the Free Software Foundation at http://www.gnu.org/licenses/gpl-3.0.html

	Oxygen is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

*/
	
	Final Class Security
		{
		
		public static function Caller()
			{
			$trace = debug_backtrace(FALSE);
			if (count($trace) < 3)
				{
				return FALSE;
				}
			else
				{
				return $trace[2]['class'];
				}
			}
		
		public static function email($string)
			{
			$string = filter_var($string, FILTER_SANITIZE_EMAIL);
			return filter_var($string, FILTER_VALIDATE_EMAIL);
			}
		
		//public static function url($string, $
		
		public static function string($string)
			{
			$string = str_split($string);
			$newstring = '';
			foreach ($string as $char)
				{
				$ord = ord($char);
				// ASCII ordinal must be the same as that for a space, underscore, dash, number or letter (upper or lowercase)
				if ($ord == 32 OR $ord == 45 OR $ord == 95 OR ($ord >= 48 AND $ord <= 57) OR ($ord >= 65 AND $ord <= 90) OR ($ord >= 97 AND $ord <= 122))
					{
					$newstring .= $char;
					}
				}
			return $newstring;
			}
		
		public static function directory($string, $depth = 1)
			{
			if (strpos($string, '/') === FALSE)
				{
				return Security::string($string);
				}
			else
				{
				$string = explode('/', $string);
				if ($string[0] == '')
					{
					array_shift($string);
					}
				$newstring = '';
				for ($n = 0; ($depth - $n) > 0; $n++)
					{
					$newstring .= Security::string($string[$n]);
					$newstring .= '/';
					}
				return substring($newstring, 0, -1);
				}
			}
		
		public static function text($string)
			{
			// TODO!!!!!
			return Security::string($string);
			}
		
		public static function integer($string)
			{
			return filter_var($string, FILTER_SANITIZE_NUMBER_INT);
			}
		
		public static function float($string)
			{
			return filter_var($string, FILTER_SANITIZE_NUMBER_FLOAT);
			}
		
		
		
		
		/*public static function ValidateHash($data, $hash)
			{
			if (file_exists('oxygen.pem') AND (extension_loaded('openssl') OR extension_loaded('bcmath') OR extension_loaded('gmp'))
				{
				if (extension_loaded('openssl'))
				{
				$key = file_get_contents('oxygen.pem');
				if (openssl_verify($data, $hash, $key) === 1)
					{
					return TRUE;
					}
				else
					{
					return FALSE;
					}
				}
			elseif (extension_loaded('bcmath'))
				{
				
			}*/
		}
?>